function [agg n] = agg_var(var,int)
% aggregates instantaneous var collected by vtls in intervals given 'int'
% INPUT:
% var: Nx2 array of the form [time][var]. N is the number of
% instataneous var collected
% int: (M+1)x1 array with the M intervals to be used
% OUTPUT:
% agg: Mx1 array with the var aggregated (in mph)
% n: number of vehicles included in each interval

N = length(var);
M = length(int) - 1;
sum_var = zeros(M,1);
n = zeros(M,1);

for i=1:N
    when = var(i,1);
    dif = when - int;
    where = find(dif>0,1,'last');
    if ~isempty(where)
        sum_var(where,1) = sum_var(where,1) + var(i,2);
        n(where,1) = n(where,1) + 1;
    end
end

agg = sum_var ./ n ;


